package com.components.project.ui.base

import android.view.ViewGroup
import com.components.base.action.TitleBarAction
import com.components.base.titlebar.TitleBar
import com.components.base.ui.fragment.BaseFragment
import com.gyf.immersionbar.ImmersionBar

abstract class ProjectBaseFragment<A : ProjectBaseActivity> : BaseFragment<A>(), TitleBarAction {

    private var mTitleBar: TitleBar? = null
    private var mImmersionBar: ImmersionBar? = null

    override fun initFragment() {
        super.initFragment()
        titleBar()?.setOnTitleBarListener(this)

        if (isStatusBarEnabled()) {
            getStatusBarConfig()?.init()
            if (titleBar() != null) {
                ImmersionBar.setTitleBar(this, titleBar())
            }
        }
    }

   open fun isStatusBarEnabled(): Boolean {
        return true
    }

    fun getStatusBarConfig(): ImmersionBar? {
        if (mImmersionBar == null) {
            mImmersionBar = createStatusBarConfig()
        }
        return mImmersionBar
    }

    private fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this).statusBarDarkFont(statusBarDarkFont()).keyboardEnable(true)
    }

    open fun statusBarDarkFont(): Boolean {
        return true
    }

    override fun titleBar(): TitleBar? {
        //这里不能判空，让他每次都去找，不然会导致标题栏占用状态栏的情况
//        if (mTitleBar == null) {
//            mTitleBar = obtainTitleBar(view as ViewGroup)
//        }
        mTitleBar = obtainTitleBar(view as ViewGroup)
        return mTitleBar
    }

}